import { defineConfig } from "vite";
import vue from "@vitejs/plugin-vue";
import legacy from "@vitejs/plugin-legacy";
import compressPlugin from "vite-plugin-compression";
const mockCookie = require("./mockCookie.txt");
// 引入
// import { visualizer } from "rollup-plugin-visualizer";

const path = require("path");
// const root = path.resolve(__dirname,'src');
// https://vitejs.dev/config/
export default defineConfig({
  base: "./",
  plugins: [
    vue(),
    legacy({
      targets: ["ie >= 11"],
      additionalLegacyPolyfills: ["regenerator-runtime/runtime"],
    }),
    compressPlugin({
      verbose: true,
      disable: false,
      threshold: 0,
      algorithm: "gzip",
      ext: ".gz",
      deleteOriginFile: false,
    }),
    // 在plugins配置数组里添加
    // visualizer({
    //   open: true, // 注意这里要设置为true，否则无效，如果存在本地服务端口，将在打包后自动展示
    //   gzipSize: true,
    //   file: "stats.html", //分析图生成的文件名
    //   brotliSize: true,
    // }),
  ],
  alias: {
    "/@/": path.resolve(__dirname, "./src"),
    "@pages": path.resolve(__dirname, "./src/pages"),
    "@utils": path.resolve(__dirname, "./src/utils"),
    "@assets": path.resolve(__dirname, "./src/assets"),
  },
  server: {
    host: "0.0.0.0",
    cors: true,
    open: true,
    port: 7772,
    proxy: {
      "/tapi": {
        target: "https://translate.julyedu.com", // 你的 API 服务器地址
        changeOrigin: true, // 是否改变请求源头
        rewrite: (path) => path.replace(/^\/tapi/, "/api"),
        secure: false, // 如果是 https 接口，需要配置这个参数
        headers: {
          Cookie: mockCookie,
        }
      },
      "/bapi": {
        target: "https://api.julyedu.com", // 你的 API 服务器地址
        changeOrigin: true, // 是否改变请求源头
        // pathRewrite: { '^/get_login_info': '' }, // 路径重写，去除请求路径中的 `/api`
        rewrite: (path) => path.replace(/^\/bapi/, "/"),
        secure: true, // 如果是 https 接口，需要配置这个参数
        headers: {
          Cookie: mockCookie,
        }
      },
    },
  },
  css: {
    preprocessorOptions: {
      less: {
        javascriptEnabled: true,
      }
    }
  },
  optimizeDeps: {
    // exclude: ['pdfjs-dist'],
  },
});
